// 登录页路径
export const LOGIN_PAGE = '/pages/login/login'

// 不需要登录的页面白名单
export const EXCLUDE_PAGE_LIST = [
  '/pages/index/index',
  '/pages/about/about',
  LOGIN_PAGE,
  '/pages/register/register',
]

// 登录相关页面列表
export const LOGIN_PAGE_LIST = [
  LOGIN_PAGE,
  '/pages/register/register',
  '/pages/forgot-password/forgot-password',
]

// TabBar 页面列表
export const TABBAR_PAGE_LIST = [
  '/pages/index/index',
  '/pages/about/about',
]

// 是否启用登录模式
export const isNeedLoginMode = false

// 路由跳转类型
export type NavigationType = 'navigateTo' | 'redirectTo' | 'reLaunch' | 'switchTab' | 'navigateBack'

// 路由跳转选项
export interface NavigationOptions {
  url: string
  type?: NavigationType
  params?: Record<string, any>
  success?: () => void
  fail?: (error: any) => void
}

// 获取完整的页面路径
export function getFullPath(path: string, params?: Record<string, any>): string {
  if (!params || Object.keys(params).length === 0) {
    return path
  }

  const query = Object.entries(params)
    .map(([key, value]) => `${key}=${encodeURIComponent(String(value))}`)
    .join('&')

  return `${path}?${query}`
}

// 判断是否为 TabBar 页面
export function isTabBarPage(path: string): boolean {
  return TABBAR_PAGE_LIST.includes(path)
}

// 判断是否为登录相关页面
export function isLoginPage(path: string): boolean {
  return LOGIN_PAGE_LIST.includes(path)
}

// 判断页面是否需要登录
export function needLogin(path: string): boolean {
  if (!isNeedLoginMode)
    return false
  return !EXCLUDE_PAGE_LIST.includes(path)
}

// 路由导航方法
export function navigateTo(options: NavigationOptions) {
  const { url, type = 'navigateTo', params, success, fail } = options
  const fullPath = getFullPath(url, params)

  const uniOptions = {
    url: fullPath,
    success,
    fail,
  }

  switch (type) {
    case 'navigateTo':
      uni.navigateTo(uniOptions)
      break
    case 'redirectTo':
      uni.redirectTo(uniOptions)
      break
    case 'reLaunch':
      uni.reLaunch(uniOptions)
      break
    case 'switchTab':
      // TabBar 页面不支持参数
      uni.switchTab({ url, success, fail })
      break
    case 'navigateBack':
      uni.navigateBack({ delta: 1 })
      break
    default:
      console.error('不支持的导航类型:', type)
  }
}

// 智能导航（自动判断导航类型）
export function smartNavigate(url: string, params?: Record<string, any>) {
  const type = isTabBarPage(url) ? 'switchTab' : 'navigateTo'
  navigateTo({ url, type, params })
}

// 导航到登录页
export function navigateToLogin(redirectUrl?: string) {
  const params = redirectUrl ? { redirect: redirectUrl } : undefined
  navigateTo({
    url: LOGIN_PAGE,
    type: 'navigateTo',
    params,
  })
}

// 登录成功后的重定向
export function redirectAfterLogin() {
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1]

  // 检查是否有重定向参数
  const query = (currentPage as any).options || {}
  const redirectUrl = query.redirect

  if (redirectUrl) {
    uni.reLaunch({
      url: decodeURIComponent(redirectUrl),
    })
  }
  else {
    // 默认跳转到首页
    uni.switchTab({
      url: '/pages/index/index',
    })
  }
}

// 退出登录后的重定向
export function redirectAfterLogout() {
  uni.reLaunch({
    url: '/pages/index/index',
  })
}
